package org.mountainpunch.ds.sparsematrix;

/**
 * A matrix data for the SparseMatrix which has integer as its data.
 * @author siddharth
 */
public class IntegerMatrixData implements MatrixDataOperable {

  private int _data;

  /**
   * Constructs a MatrixData which contains integer as its data.
   * @param data the integer data.
   */
  public IntegerMatrixData(int data) {
    _data = data;
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable multiply(MatrixDataOperable mdata) {

    if (mdata instanceof IntegerMatrixData) {
      IntegerMatrixData imd = (IntegerMatrixData) mdata;
      return new IntegerMatrixData(_data * imd.getData());
    }

    throw new RuntimeException("MatrixDataOperable not found");
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable add(MatrixDataOperable mdata) {

    if(mdata == null) {
      return new IntegerMatrixData(_data) ;
    }

    if (mdata instanceof IntegerMatrixData) {

      IntegerMatrixData imd = (IntegerMatrixData) mdata;
      return new IntegerMatrixData(_data + imd.getData());
    }

    throw new RuntimeException("Not an IntegerMatrixData");
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable subtract(MatrixDataOperable mdata) {
    if(mdata == null) {
      return new IntegerMatrixData(_data);
     }
    if (mdata instanceof IntegerMatrixData) {
      IntegerMatrixData imd = (IntegerMatrixData) mdata;
      return new IntegerMatrixData(_data - imd.getData());
    }

    throw new RuntimeException("Not an IntegerMatrixData");
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public MatrixDataOperable interpolate(MatrixDataOperable mdata, float t) {
    if(mdata == null) {
      throw new NullPointerException("MatrixDataOperable operand is null");
    }

    if (mdata instanceof IntegerMatrixData) {
      IntegerMatrixData imd = (IntegerMatrixData) mdata;
      return new IntegerMatrixData((int) (_data + t * (imd.getData() - _data)));
    }

    throw new RuntimeException("Not an IntegerMatrixData");
  }

  /**
   * Returns the data contained in this matrix data.
   * @return the integer matrix data value.
   */
  public int getData() {
    return _data;
  }

  /**
   * {@inheritDoc }
   */
  @Override
  public String toString() {
    return String.valueOf(_data);
  }

  /**
   * {@inheritDoc }
   */
  public MatrixDataOperable negate() {
    return new IntegerMatrixData(-_data) ;
  }
}
